import Router from '../../utils/router';
import base from '../../config/base.js';
import service from '../../services/choose';
import Record from '../../utils/page-record.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_store_auth: base.static + 'store/store-auth.png',
    icon_search: base.static + 'choose/search3.png',
    current: '',
    sortby: 'random_order', //排序
    order: 'desc', //升序
    chooseNav: '',
    navList: '',
    goodsList: [],
    loadAllBloon: true,
    endAllBloon: true,
    pageAll: 0,
    nomore: true,
    scrolltopnum: '',
    user_id: '',
    store_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let user_info = wx.getStorageSync('user_info') || { lastView: { seid: ''}, id:''};
    that.setData({
      user_id: user_info.id,
      store_id: user_info.lastView.seid,
      
    })
    that.getNavList();
  },
  // 点击导航栏
  navChoose: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    that.setData({
      current: id,
      goodsList: [],
      loadAllBloon: true,
      endAllBloon: true,
      pageAll: 0,
      nomore: true,
    })
    that.classifyList(1, id);
    if (id == 1) {
      that.setData({
        chooseNav: 'into' + id
      })
    } else if (id == 2) {
      that.setData({
        chooseNav: 'into' + id
      })
    } else if (id == 5) {
      that.setData({
        chooseNav: 'into' + id
      })
    } else if (id == 18) {
      that.setData({
        chooseNav: 'into' + id
      })
    }
  },
  //点击排序
  sort: function (e) {
    let that = this;
    let order = that.data.order;
    let sort = e.currentTarget.dataset.sort
    that.setData({
      sortby: e.currentTarget.dataset.sort
    })
    if (sort == 'sale_price') {
      if (order == "desc") {
        that.setData({
          order: 'asc'
        })
      } else {
        that.setData({
          order: 'desc'
        })
      }
    } else {
      that.setData({
        sort: 'desc'
      })
    }
    that.setData({
      goodsList: [],
      loadAllBloon: true,
      endAllBloon: true,
      pageAll: 0,
      nomore: true,
    })
    that.classifyList(1, that.data.current);
  },
  // 分类列表
  getNavList: function () {
    let that = this;
    let params = {
      app_id: 1
    };
    service.categorylist(params, function (res) {
      if (res && res.code == 200) {
        that.setData({
          current: res.data[0].id,
          navList: res.data,
          current: res.data[0].id
        });
        that.classifyList(1,res.data[0].id)
      }
    });
  },
  // 分类下属列表
  classifyList: function(page,typeid){
    let that = this;
    let params = {
      page: page,
      per_page: 10,
      cate_id: typeid,
      sortby: that.data.sortby,
      order: that.data.order,
      store_id: that.data.store_id
    };
    service.customercategory(params, function (res) {
      if (res && res.code == 200) {
        let stolistAll = that.data.goodsList.concat(res.data);
        stolistAll.forEach((item, index) => {
          stolistAll[index].src = item.goods_pic[0].url
          stolistAll[index].goods_id = item.goods_pic[0].goods_id
        })
        that.setData({
          goodsList: stolistAll,
          pageAll: page + 1,
          endAllBloon: true
        });
        if (res.data == "") {
          that.setData({
            loadAllBloon: false,
            nomore: false,
          })
        }
      }
      wx.stopPullDownRefresh();
    });
  },
  //去搜索页面
  goSearch: function () {
    Router.go("search", ``);
    let p = getCurrentPages().pop();
    Record.clickButton(p, 'go_search_button');
  },
  //监听页面高度
  scrollTimer: null,
  onPageScroll: function (res) {
    let that = this;
    if (that.scrollTimer) { clearTimeout(that.scrollTimer) };
    that.scrollTimer = setTimeout(() => {
      that.setData({
        scrolltopnum: res.scrollTop
      })
    }, 500);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    if (that.scrollTimer){
      clearTimeout(that.scrollTimer);
    }
    that.setData({
      goodsList: [],
      loadAllBloon: true,
      endAllBloon: true,
      pageAll: 0,
      nomore: true,
    })
    that.classifyList(1, that.data.current);
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.loadAllBloon && that.data.endAllBloon) {
      that.setData({
        endAllBloon: false
      })
      that.classifyList(that.data.pageAll, that.data.current);
    }
  },
})